<template>
  <div class="other-user">
    <div class="user-nav">
      <div class="user-avatar">
        <img :src="userData.avatar || defaultAvatar" />
      </div>
      <div class="user-nickname">{{ userData.nickName }}</div>
      <div class="user-phone">
        <i class="el-icon-phone"></i>{{ userData.phone || "暂无号码" }}
      </div>
      <div class="user-register-time">
        <i class="el-icon-date"></i>{{ userData.createTime }}注册
      </div>
    </div>
    <div class="user-main">
      <div class="introduce">
        <h3>用户简介</h3>
        <p>
          <i class="el-icon-chat-line-square"></i>
          {{ userData.introduction || "暂无简介" }}
        </p>
      </div>
      <div class="collect">
        <el-tabs @tab-click="handleClick" style="height: 100%">
          <el-tab-pane
            label="收藏"
            style="height: 100%"
            v-loading="dataLoading"
          >
            <div v-if="dataList.length > 0" style="width: 100%; height: 100%">
              <CommonCollectItem :dataList="dataList"></CommonCollectItem>
              <el-pagination
                v-show="dataList.length > 0"
                layout="prev, pager, next"
                :current-page="currentPage"
                @current-change="handleCurrentChange"
                :total="total"
                :page-count="7"
                background
                style="text-align: right; margin-top: 20px"
              >
              </el-pagination>
            </div>
            <div class="empty" v-else="!dataList.length > 0">
              <el-empty description="暂无数据，快去添加吧~"> </el-empty>
            </div>
          </el-tab-pane>
          <el-tab-pane
            label="看过"
            style="height: 100%"
            v-loading="dataLoading"
          >
            <div v-if="dataList.length > 0" style="width: 100%; height: 100%">
              <CommonCollectItem :dataList="dataList"></CommonCollectItem>
              <el-pagination
                v-show="dataList.length > 0"
                layout="prev, pager, next"
                :page-size="pageSize"
                :current-page="currentPage"
                @current-change="handleCurrentChange"
                :total="total"
                background
                style="text-align: right; margin-top: 20px"
              >
              </el-pagination>
            </div>
            <div class="empty" v-else="!dataList.length > 0">
              <el-empty description="暂无数据，快去添加吧~"> </el-empty></div
          ></el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>
<script>
import CommonCollectItem from "@/components/CommonCollectItem.vue";
import defaultAvatar from "@/assets/defaultAvatar.png";
import { getUserInfo } from "@/api/user";
import { getWantOrSeeList } from "@/api/common";
export default {
  name: "UserView",
  data() {
    return {
      page: 1,
      pageSize: 12,
      total: 0,
      currentPage: 1,
      want: 1, // 是否是收藏
      see: 0, // 是否是看过
      // 加载
      dataLoading: false,
      // 用户信息
      userData: {},
      // 数据集
      dataList: [],
      defaultAvatar,
    };
  },
  props: ["id"],
  components: {
    CommonCollectItem,
  },
  created() {
    this.getUserInfo();
    this.getWantOrSeeList();
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      const res = await getUserInfo(this.id);
      console.log(res.data);

      this.userData = res.data;
    },
    // 获取收藏或看过列表
    async getWantOrSeeList() {
      try {
        this.dataLoading = true;
        const res = await getWantOrSeeList({
          page: this.page,
          pageSize: this.pageSize,
          wantStatus: this.want,
          seeStatus: this.see,
          userId: this.id,
        });
        this.dataList = res.data.records;
        this.total = res.data.total;
      } catch {
        this.$message.error("获取数据失败");
      } finally {
        this.dataLoading = false;
      }
    },
    // tab切换
    handleClick(tab, event) {
      this.page = 1;
      const tabName = tab.label;
      if (tabName === "收藏") {
        this.want = 1;
        this.see = 0;
        this.getWantOrSeeList();
      } else if (tabName === "看过") {
        this.want = 0;
        this.see = 1;
        this.getWantOrSeeList();
      }
    },
    // 当前页改变时触发
    handleCurrentChange(page) {
      this.page = page;
      this.getWantOrSeeList();
    },
  },
};
</script>

<style lang="less" scoped>
.other-user {
  width: 92%;
  height: 100%;
  margin: 0 auto;
  padding-top: 10px;
  display: flex;
  justify-content: space-around;
  .user-nav {
    width: 20%;
    height: 700px;
    background-color: #fff;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .user-avatar {
      margin-top: 20px;
      width: 100px;
      height: 100px;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .user-nickname {
      margin-top: 8px;
      width: 120px;
      font-size: 14px;
      // 超出部分隐藏
      overflow: hidden;
      // 超出部分显示省略号
      text-overflow: ellipsis;
      // 不换行
      white-space: nowrap;
      color: #999;
      text-align: center;
    }
    .user-phone,
    .user-register-time {
      margin-top: 4px;
      font-size: 13px;
      color: #999;
      i {
        margin-right: 4px;
      }
    }
  }

  .user-main {
    width: 78%;
    height: 700px;
    background-color: #fff;
    border-radius: 8px;
    .introduce {
      padding: 14px 18px;
      h3 {
        border-bottom: 1px solid #aaa;
        padding-bottom: 2px;
      }
      p {
        height: 60px;
        padding: 0px 10px;
        margin-top: 4px;
        color: #666;
        font-size: 14px;
        i {
          margin-right: 4px;
        }
      }
    }
    .collect {
      padding: 0 10px;
    }
  }
}
</style>